package office.poi.excel.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import office.poi.excel.annotation.ExcelColumn;
import office.poi.excel.bean.ExcelColumnBean;
import office.poi.excel.enums.PropertyType;


public class ScanBeanUtil {
	/**
	 * 必填行在list中的的索引
	 */
	public static int REQUIRED_FLAG = -1;
	/**
	 * 将 Excel的列名与bean字段绑定对应
	 * @param clazz
	 * @return
	 */
	public static List<ExcelColumnBean> getWriteMethod(Class<?> clazz)
	{
		  List<ExcelColumnBean> list = new ArrayList<ExcelColumnBean>();
		  
		  Field[] fields = clazz.getDeclaredFields();  
		  
		  ExcelColumnBean ecb = null;
		  for (Field f:fields) {  
			  ExcelColumn ec = f.getAnnotation(ExcelColumn.class);
			  if(ec!=null)
			  {
				  ecb = new ExcelColumnBean();
				  //实体字段名
				  String beanName = f.getName();
				  //得到Excel列名
				  String columnName =  ec.columnName();
                  //该列是否必填
				  Boolean required = ec.required();
				  //该列验证类型
				  PropertyType propertyType = ec.propertyType();
				  // 该列字符长度
				  Integer length = ec.length();
				  // 该列小数点前多少位
				  Integer precision = ec.precision();
				  //该列小数点后多少位
				  Integer scale =ec.scale();
				  ecb.setBeanName(beanName);
				  ecb.setColumnName(columnName);
				  ecb.setRequired(required);
				  ecb.setPropertyType(propertyType);
				  ecb.setLength(length);
				  ecb.setPrecision(precision);
				  ecb.setScale(scale);
				  if(required)REQUIRED_FLAG = list.size();
				  list.add(ecb);
			  } 
		  }
		  return list;
	}
}
